﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Net;
using System.Windows.Forms;
using RxjhServer.DbClss;
using RxjhServer.Network;

namespace RxjhServer
{
	public partial class BinIP : Form
	{
		public BinIP()
		{
			this.InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			World.封IP = this.checkBox1.Checked;
			World.游戏登陆端口最大连接数 = (int)this.numericUpDown1.Value;
			World.自动连接时间 = (int)this.numericUpDown2.Value;
			World.游戏登陆端口最大连接时间数 = (int)this.numericUpDown3.Value;
			World.自动开启换端口 = this.checkBox5.Checked;
			World.断开连接 = this.checkBox2.Checked;
			World.加入过滤列表 = this.checkBox3.Checked;
			World.关闭连接 = this.checkBox4.Checked;
			Config.IniWriteValue("GameServer", "封IP", this.checkBox1.Checked.ToString());
			Config.IniWriteValue("GameServer", "自动开启换端口", this.checkBox5.Checked.ToString());
			Config.IniWriteValue("GameServer", "断开连接", this.checkBox2.Checked.ToString());
			Config.IniWriteValue("GameServer", "加入过滤列表", this.checkBox3.Checked.ToString());
			Config.IniWriteValue("GameServer", "关闭连接", this.checkBox4.Checked.ToString());
			Config.IniWriteValue("GameServer", "游戏登陆端口最大连接数", this.numericUpDown1.Value.ToString());
			Config.IniWriteValue("GameServer", "自动连接时间", this.numericUpDown2.Value.ToString());
			Config.IniWriteValue("GameServer", "游戏登陆端口最大连接时间数", this.numericUpDown3.Value.ToString());
			base.Close();
		}

		private void BinIP_Load(object sender, EventArgs e)
		{
			this.bind();
			this.bind2();
			this.bind3();
		}

		public void bind3()
		{
			this.checkBox1.Checked = World.封IP;
			this.numericUpDown1.Value = World.游戏登陆端口最大连接数;
			this.numericUpDown2.Value = World.自动连接时间;
			this.numericUpDown3.Value = World.游戏登陆端口最大连接时间数;
			this.checkBox5.Checked = World.自动开启换端口;
			this.checkBox2.Checked = World.断开连接;
			this.checkBox3.Checked = World.加入过滤列表;
			this.checkBox4.Checked = World.关闭连接;
		}

		public void bind()
		{
			this.listView1.Items.Clear();
			foreach (IPAddress current in World.BipList)
			{
				string[] items = new string[]
				{
					current.ToString()
				};
				this.listView1.Items.Insert(this.listView1.Items.Count, new ListViewItem(items));
			}
		}

		public void bind2()
		{
			this.listView2.Items.Clear();
			foreach (NetState current in World.list.Values)
			{
				string[] items = new string[]
				{
					current.WorldId.ToString(),
					current.ToString()
				};
				this.listView2.Items.Insert(this.listView2.Items.Count, new ListViewItem(items));
			}
		}

		private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (this.listView1.SelectedItems.Count > 0)
			{
				for (int i = 0; i < this.listView1.SelectedItems.Count; i++)
				{
					string text = this.listView1.SelectedItems[i].SubItems[0].Text;
					World.BipList.Remove(IPAddress.Parse(text));
					this.bind();
				}
			}
		}

		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			if (this.listView2.SelectedItems.Count > 0)
			{
				for (int i = 0; i < this.listView2.SelectedItems.Count; i++)
				{
					string text = this.listView2.SelectedItems[i].SubItems[0].Text;
					NetState netState;
					if (World.list.TryGetValue(int.Parse(text), out netState))
					{
						netState.Dispose();
					}
					this.bind2();
				}
			}
		}
	}
}
